class ArticlesController < ApplicationController
  # GET /articles
  # GET /articles.xml

  def index
    @articles = Article.all
    @user = User.all
	@status = Status.all
	@access_level = AccessLevel.all
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @articles }
    end
  end

  # GET /articles/1
  # GET /articles/1.xml
  def show
    @article = Article.find(params[:id])
    @event = Event.all
    @status = Status.all
    @access_level = AccessLevel.all
    @user = User.all
    
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @article }
    end
  end

  # GET /articles/new
  # GET /articles/new.xml
  def new
    @article = Article.new
    @event = Event.all
    @status = Status.all
    @access_level = AccessLevel.all
    @user = User.all
    
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @article }
    end
  end

  # GET /articles/1/edit
  def edit
    @article = Article.find(params[:id])
     @event = Event.all
    @status = Status.all
    @access_level = AccessLevel.all
    @user = User.all
    
  end

  # POST /articles
  # POST /articles.xml
  def create
    @article = Article.new(params[:article])

    respond_to do |format|
      if @article.save
        format.html { redirect_to(@article, :notice => 'Article was successfully created.') }
        format.xml  { render :xml => @article, :status => :created, :location => @article }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @article.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /articles/1
  # PUT /articles/1.xml
  def update
    @article = Article.find(params[:id])

    respond_to do |format|
      if @article.update_attributes(params[:article])
        format.html { redirect_to(@article, :notice => 'Article was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @article.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /articles/1
  # DELETE /articles/1.xml
  def destroy
    @article = Article.find(params[:id])
    @article.destroy

    respond_to do |format|
      format.html { redirect_to(articles_url) }
      format.xml  { head :ok }
    end
  end
  
  def delete_selected
    @ids = params[:ids]
    
    @ids.each do |object|
      @a = Article.find(object[0])
      @a.destroy
    end
    
    respond_to do |format|
      format.html { redirect_to(articles_url, :notice => "thanh cong") }
      format.xml  { head :ok }
    end
  end

  def search
    @bien=params[:txtText]
	@bien1=params[:article]
	@bien1=@bien1[:user_id]
	@bienngay=params[:start_date]
	@articles = Article.all
    @user = User.all
	@status = Status.all
	@access_level = AccessLevel.all
	
    # @article = Article.where("title like '%'")
    @articles =  Article.where(" title like '%#{@bien}%' and user_id = '#{@bien1}' and start_publishing = '#{@bienngay}'")
     respond_to do |format|
       #redirect_to :action =>'index'
      format.html {render 'index.html.erb'}
      #format.xml  { render :xml => @article }
    end
    
  end  
end


